<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use function foo\func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Mail\Message;
use Hash;

class UserController extends BaseController
{
    //页面展示
    public function index()
    {
        $data = User::orderBy('id', 'asc')->withTrashed()->paginate($this->pagesize);
        return view('admin.user.index', compact('data'));
    }

    //新增页面展示
    public function create()
    {
        return view('admin.user.create');
    }

    //新增页面逻辑
    public function add(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|unique:users,username',
            'truename' => 'required',
            'password' => 'required|confirmed',
            'phone' => 'phone|unique:users,phone',
            'email' => 'email|unique:users,email'
        ]);
        $post = $request->except(['_token', 'password_confirmation', 'url']);
        $pwd = $post['password'];
        $post['password'] = bcrypt($post['password']);
        $res = User::create($post);
        if ($res) {
            //富文本页面
            \Mail::send('mail.userAdd', compact('res', 'pwd'), function (Message $message) use ($res) {
                $message->to($res->email);
                $message->subject('添加用户');
            });
            return redirect(route('admin.user.index'))->with('success', '新增成功!');
        }
    }

    //删除用户逻辑
    public function del(int $id)
    {
        $res = User::find($id)->delete();
        if (!$res) {
            return ['status' => 0, 'msg' => '删除失败！'];
        }
        return ['status' => 0, 'msg' => '删除成功'];
    }

    //恢复用户
    public function reset(int $id)
    {
        $res = User::onlyTrashed()->where('id', $id)->restore();
        return redirect(route('admin.user.index'))->with('success', '恢复成功!');
    }

    //批量删除用户
    public function delAll(Request $request)
    {
        $id = $request->get('id');
        User::destroy($id);
        return ['status' => 0, 'msg' => '删除成功'];
    }

    //修改数据页面展示
    public function edit(int $id)
    {
        $data = User::find($id);
        if ($data) {
            return view('admin.user.edit', compact('data'));
        }
        return redirect(route('admin.user.index'))->withErrors(['error' => '该用户存在问题!']);
    }

    //修改用户逻辑
    public function update(Request $request, int $id)
    {
        $res = User::find($id);
        $data = $request->only(
            'nickname',
            'sex',
            'phone',
            'email',
            'truename',
            'spassword',
            'password'
        );
        //验证与原密码是否相同
        $sta = Hash::check($data['spassword'], $res->password);
        if (!$sta) {
            return redirect(route('admin.user.edit', ['id' => $id]))->withErrors(['error' => '原密码错误!']);
        }
        unset($data['spassword']);
        if (empty($data['password'])) {
            unset($data['password']);
        }
        $data['password'] = bcrypt($data['password']);
        $status = $res->update($data);
        if (!$status) {
            return redirect(route('admin.user.edit', ['id' => $id]))->withErrors(['error' => '修改失败!']);
        }
        return redirect(route('admin.user.index'))->with(['success' => '修改成功']);
    }

    //用户赋予角色
    public function role(Request $request, User $user)
    {
        //提交修改
        if ($request->isMethod('POST')) {
            $role_id = $this->validate($request, [
                'role_id' => 'required'
            ]);
            $user->update($role_id);
            return redirect(route('admin.user.index'));
        }
        //页面展示
        $roleAll = Role::all()->toArray();
        return view('admin.user.role', compact('roleAll', 'user'));
    }
}
